<?php
C_base::yn_file('base'.DS.'htmlbase.php');
class weixin extends htmlbase{
	
	var $_vpath = '';//pc/phone文件夹下的路径
	var $appid = 'wxb436d61e09f578f2';//公众号唯一标识
	var $secret = '';//公众号的appsecret
	var $access_token = '?access_token=';
	var $wxapi_url='https://api.weixin.qq.com/cgi-bin';
	
	function __construct(){
		parent::__construct();
		$this->access_token .= self::get_token();//获取access_token
	}
	
	function getcallbackip(){//获取微信服务器IP地址
		$url = $this->wxapi_url.'/getcallbackip'.$this->access_token;
		$rs = $this->http_curl($url);
		return $rs;
	}
	
	function get_token($type = 0){//获取access_token
		if(@func::Ses('token_time') && @func::Ses('access_token') && ((func::Ses('token_time')+7200) > time()) && $type == 0){//如果未超过2个小时，则不重新获取
			return func::Ses('access_token');
		}else{//如果超过2个小时，重新获取
			$url = $this->wxapi_url.'/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->secret;
			$arr = $this->http_curl($url);
			if(@$arr['access_token']){//access_token
				func::Ses('token_time',time());//存储当前时间
				func::Ses('access_token',$arr['access_token']);
				return $arr['access_token'];
			}
		}
	}
	
	/**
	 * 生成签名
	 * noncestr（随机字符串）
	 * 有效的jsapi_ticket
	 * timestamp（时间戳）
	 * url（当前网页的URL，不包含#及其后面部分）
	 */
	function get_signature(){
		$jsapi_ticket = self::jsapi_ticket();//jsapi_ticket
		// 注意 URL 一定要动态获取，不能 hardcode.
		$http = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
		$url = $http.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$timestamp = time();
		$noncestr = util::rand_sum('aA9','16');
		$str = 'jsapi_ticket='.$jsapi_ticket.'&noncestr='.$noncestr.'&timestamp='.$timestamp.'&url='.$url;
		$signature = sha1($str);//sha1签名
		$rs = array('appid'=>$this->appid,'timestamp'=>$timestamp,'noncestr'=>$noncestr,'signature'=>$signature);
		return $rs;
	}
	
	function jsapi_ticket(){//获得jsapi_ticket
		if(@func::Ses('jsapi_ticket_time') && @func::Ses('jsapi_ticket') && (func::Ses('jsapi_ticket_time')+7200) > time()){//如果未超过2个小时，则不重新获取
			return func::Ses('jsapi_ticket');
		}else{//如果超过2个小时，重新获取
			$access_token = self::get_token();
			$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=jsapi';
			$arr = self::http_curl($url);
			if(@$arr['ticket']){//jsapi_ticket
				func::Ses('jsapi_ticket_time',time());//存储当前时间
				func::Ses('jsapi_ticket',$arr['ticket']);
				return $arr['ticket'];
			}
		}
	}
	
	function http_curl($url){//http请求
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//https请求 不验证证书
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//https请求 不验证host
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//将抓取代码存储到字符串
		$nr = curl_exec($ch);
		curl_close($ch);
		$nr = json_decode($nr);//json数据转换为对象
		$nr = util::object_array($nr);//将对象转换为数组
		if(@$nr['errcode'] == '40001'){//如果别处重新请求了access_token,则此处再次请求access_token。
			self::get_token(1);
		}
		return $nr;
	}
	
	
	function wxzf(){
		ini_set('date.timezone','Asia/Shanghai');
		//error_reporting(E_ERROR);
		C_base::yn_file('base'.DS.'wx'.DS.'lib'.DS.'WxPay.Api.php');
		C_base::yn_file('base'.DS.'wx'.DS.'example'.DS.'WxPay.NativePay.php');
		C_base::yn_file('base'.DS.'wx'.DS.'example'.DS.'log.php');
		//require_once "../lib/WxPay.Api.php";
		//require_once "WxPay.NativePay.php";
		//require_once 'log.php';
		$notify = new NativePay();
		$url1 = $notify->GetPrePayUrl("123456789");
	}
}